<?php
/**
 * SemesterCourse Model
 *
 * Utilizes the Data Mapper pattern to persist data. Represents a single
 * semester_course entry
 *
 * @version $Id: SemesterCourse.php 87 2010-05-01 09:22:28Z yingkuang.chen@gmail.com $
 * @license New BSD License
 * @uses Semester_Model_SemesterCourseMapper
 * @package semester
 * @subpackage Model
 */
class Semester_Model_SemesterCourse extends Semester_Model_Doctrine_BaseSemesterCourse
{

    const   RECURSIVE = 1;
    const   NO_RECURSIVE = 0;


    /**
     *  取得學年科目設定
     * @param $year 學年
     * @param $semester 學期
     * @param $grade 年級
     * @return array
     */
    public function getSemesterSubject($year,$semester=null,$grade)
    {

        if ($semester==null){
            $semester = substr($year,-1);
            $year=substr($year,0,-1);
        }

        $q = $this->getTable()->createQuery('a')
        ->where('year=?', $year)
        ->andwhere('semester=?', $semester)
        ->andwhere('grade=?', $grade)
        ->andwhere('enable=?', 'y')
        ->orderBy('grade, scope_rank, rank');

        $row = $q->fetchArray();

        $arr = array();
        foreach ($row as $val) {
            $scope_arr = explode("-",$val['scope']);

            if (count($scope_arr)>1) {
                $arr[$scope_arr[0]][] = $val;
            }
            else
            $arr[$val['scope']][] = $val;
        }
        //print_r($arr);
        return $arr;
    }

    /**
     * 取得九年一貫科目
     *
     * @return array
     */
    public function getScoreName($flag = self::RECURSIVE)
    {
        $cache = Zend_Registry::get('cache');
        $school = (object)$cache->load('school');
        if ($school){
            $length = $school->length;
            $county = $school->county;
            $locale = Bootstrap::$config['resources']['locale']['default'];
            $file = dirname(__FILE__)."/../languages/$locale/course.xml";
            $course_xml = simplexml_load_file($file);
            if (!$course = $course_xml->xpath("//course[@area='$county']"))
            $course = $course_xml->xpath("//course[@area='default']");


            $scope_arr = array();
            foreach($course[0]->grade as $val){
                $attr = $val->attributes();
                $arr = explode("-",$attr['id']);
                for($i=$arr[0];$i<=$arr[1];$i++) {
                    foreach($val->scope as $ss) {
                        $name = (string)$ss->attributes()->name;
                        $rank = 1;
                        if ($ss->subject and $flag) {
                            foreach ($ss->subject as $val_sub) {
                                $required = (string)$val_sub->attributes()->required;
                                $sname = (string)$val_sub->attributes()->name;
                                $scope = $name.'-'.$sname;
                                $scope_arr[$i][]= array('scope'=>$scope,'rank'=>$rank++,'name'=>$sname,'required'=> $required);
                            }
                        }else {
                            $required = (string) $ss->attributes()->required;
                            $scope_arr[$i][]= array('scope'=>$name,'name'=>$name,'rank'=>$rank,'required'=> $required);
                        }
                    }
                }
            }
            return $scope_arr;
        }
    }


}
